package com.letv.work.cp_info;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.letv.bean.SummaryBean;
import com.letv.dataprocess.DataProcessInterface;
import com.letv.dataprocess.cp_info.CpInfoDataProcess;
import com.letv.download.cpinfo.CpInfoDataDonwload;
import com.letv.util.DateUtil;
import com.letv.work.JobInterface;

/**
 * CP business offline task job class
 */
public class CpInfoJob implements JobInterface {

    /**
     * Logger
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(CpInfoJob.class);

    /**
     * do job
     * @param workerName String, the worker's name to do this job
     * @return SummaryBean, the result information of this job
     */
    @Override
    public SummaryBean doJob(String workerName) {
        if(StringUtils.isBlank(workerName)) {
            LOGGER.error("methed doJob get a blank string for workerName");
            return null;
        }

        long startTime = 0;
        String startTimeFormat = null;
        try {
            startTime = System.currentTimeMillis();
            startTimeFormat = DateUtil.getInstance().formatTime(startTime);
        } catch(Exception e) {
            LOGGER.error("methed doJob set start time error for " + workerName, e);
        }

        new CpInfoDataDonwload().download();

        SummaryBean result = null;
        try {
            DataProcessInterface dataprocess = new CpInfoDataProcess();
            result = dataprocess.process(workerName);
        } catch(Exception e) {
            LOGGER.error("methed doJob has errors for " + workerName, e);
        }

        long endTime = 0;
        String endTimeFormat = null;
        try {
            endTime = System.currentTimeMillis();
            endTimeFormat = DateUtil.getInstance().formatTime(endTime);
        } catch(Exception e) {
            LOGGER.error("methed doJob set start time error for " + workerName, e);
        }

        LOGGER.info("cp info start time is : " + startTimeFormat);
        LOGGER.info("cp info end time is : " + endTimeFormat);
        LOGGER.info("cp info cost time : " + ((endTime - startTime) / 1000) + "s");
        return result;
    }
}
